$(function () {
    init_data();
    $("#collapseOne").collapse({toggle: false});
    $.getJSON(ctx+"bigEpExport/getlimitBigRecords",{},function (r) {
        var recodsArray = new Set();
        $.each(r,function (index,value) {
            recodsArray.add(value.epBatch);
        });
        $( "#records-autocomplete" ).autocomplete({
            source: Array.from(recodsArray),
            select: function( event, ui ) {
                $("#records-autocomplete").val(ui.item.label);
                $JuXue.refreshTable("bigEmpRecordsTable");
            }
        });
    });
    $("#advance-daterange span").html(moment().subtract("days",29).format("YYYY-MM-DD")+" - "+moment().format("YYYY-MM-DD"));
    $('#advance-daterange').daterangepicker({
        timePicker: true, //显示时间
        timePicker24Hour: true, //时间制
        timePickerSeconds: true, //时间显示到秒
        startDate: moment().hours(0).minutes(0).seconds(0), //设置开始日期
        endDate: moment(new Date()), //设置结束器日期
        maxDate: moment(new Date()), //设置最大日期
        "opens": "center",
        ranges: {
            '今天': [moment(), moment()],'昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            '上周': [moment().subtract(6, 'days'), moment()],
            '前30天': [moment().subtract(29, 'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        showWeekNumbers: true,
        locale: {
            format: "YYYY-MM-DD HH:mm:ss", //设置显示格式
            applyLabel: '确定', //确定按钮文本
            cancelLabel: '取消', //取消按钮文本
            customRangeLabel: '自定义',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'
            ],
            firstDay: 1
        },
    }).on('apply.daterangepicker', function(ev, picker) {
        var starttime = picker.startDate.format('YYYY-MM-DD');
        var endtime = picker.endDate.format('YYYY-MM-DD');
        $("#advance-daterange span").html(starttime+" - "+endtime);
        $("#starttime").val(starttime)
        $("#endtime").val(endtime);
        $JuXue.refreshTable("bigEmpRecordsTable");
    });

    $('#records-autocomplete').live('input propertychange', function() {
        $JuXue.refreshTable("bigEmpRecordsTable");
    });
    $('#records_direct').live('input propertychange', function() {
        $JuXue.refreshTable("bigEmpRecordsTable");
    });
});

/**初始化数据**/
function init_data(){
    var settings = {
        pageSize: 15,
        url: ctx + "bigEpExport/getAllAIRecords",
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                epBatch: $("#records-autocomplete").val(),
                epDirector :$("#records_direct").val(),
                startTimers:$("#starttime").val(),
                endTimer :$("#endtime").val(),
            };
        },
        columns: [{
            checkbox: true
        },
            {
                field: 'epBatch',
                title: 'AI资源批次',
                clickToSelect:false,
                class:'text-center',
                formatter:function (value, row, index) {
                    return "<a href='javascript:void(0)' onclick='viewAiEmpDetailRecord(this)' data-click='"+row.epBatchUuid+"'>"+value+"</a>";
                }
            }, {
                field: 'epDirector',
                class:'text-center',
                title: '所属专员'
            }, {
                field: 'epNum',
                class:'text-center',
                title: '导入数量',
                formatter:function (value, row, index) {
                    var falure = row.epFNum;
                    return " <span   class=\"email-btn m-r-15\" >导入 "+value+" 个</span>\n" +
                        "<a href=\"javascript:void(0);\" onclick='viewFaluiAiEmpDetail(this)' data-click='"+row.epUuid+"' class=\"email-btn\"  > 失败 "+falure+" 个</a>"+
                        "<a href=\"javascript:void(0);\" onclick='downLoadError(this)' data='"+row.epUuid+"' class='email-btn  m-l-15'  >下载</a>";
                }
            }, {
                field: 'epOption',
                class:'text-center',
                title: '操作人'
            },{
                field: 'epTime',
                class:'text-center',
                title: '导入时间'
            }]
    };
    $JuXue.initTable('bigEmpRecordsTable', settings);
}
function downLoadError(btn) {
    var uuid = $(btn).attr("data");
    $.post(ctx+"aiEpResoure/downloadList",{"uuid":uuid},function (r) {
        if (r.code === 0) {
            window.location.href = "file/download?fileName=" + r.msg + "&delete=" + true;
        } else {
            $JuXue.n_danger(r.msg);
        }
    })
}
